import React, { Component } from 'react'
import qs from 'qs'
import {goodsRequest} from '../request/cate_request'
import Header from '../components/DefaultHeader'
export default class GoodsList extends Component {
    //放到哪里了
    //this.props
    //哪里获取比较合适
    state = {
        type_name: "",
        goods_list: []
    }
    UNSAFE_componentWillMount(){
        const {type_id,type_name} = qs.parse(this.props.location.search.substring(1))
        this.setState({type_name})
        goodsRequest(type_id).then(res=>{
            this.setState({goods_list:res.list})
        })
    }
    render() {
        const {type_name,goods_list} = this.state
        return (
            <div className="goods-list-container">
                <Header title={type_name}/>
                <div className="goodslist">
                    {
                        goods_list.map(item=>(
                            <div className="item" key={item.id}>
                                <img onClick={()=>this.props.history.push('/goods_detail/?id='+item.id)} src={this.$static_host+item.img} alt="" />
                                <div>
                                    <h5 onClick={()=>this.props.history.push('/goods_detail/?id='+item.id)}>{item.goodsname}</h5>
                                    <p className="price">&yen;{item.price}</p>
                                    <p className="market-price">&yen;{item.market_price}</p>
                                    <button className="btn-cart">加入购物车</button>
                                </div>
                            </div>
                        ))
                    }
                </div>
            </div>
        )
    }
}
